<?php

class ShowAction extends CAction
{
    public $user;
    public $kitchen;
    public $recipes=array();
    public $reports=array();
    
    private function loadUser($userId)
    {
        $this->user=User::loadObjectById($userId);
        if($this->user===null)
            throw new CHttpException(404,'The requested user does not exist.');    
    }

    private function loadKitchen()
    {
        $this->kitchen=Kitchen::loadObjectById($this->user->id);
        if($this->kitchen===null)
            throw new CHttpException(404,'The requested kitchen does not exist.');
    }
    
    private function loadRecipes()
    {    
        return $this->recipes=Recipe::loadPublishedObjectsByUserId($this->user->id, array('limit'=>1));
    }
    
    private function loadReports()
    {
    }

    public function run()
    {
        if(!isset($_GET['id'])) {
            if(Yii::app()->user->isGuest) {
                Yii::app()->user->loginRequired();
            }
            else {
                $userId=Yii::app()->user->id;
            }
        }
        else {
            $userId=$_GET['id'];     
        }

        $this->loadUser($userId);
        $this->loadKitchen();
        $this->loadRecipes();
        $this->loadReports();
        
        $this->controller->render('show',array(
            'user'=>$this->user,
            'kitchen'=>$this->kitchen,
            'recipes'=>$this->recipes,
            'reports'=>$this->reports,
            'edit'=>Yii::app()->user->id==$this->user->id,
        ));
    }
}
  
?>